/*
 * LtiBandPass.cpp
 *
 *  Created on: May 29, 2009
 *      Author: flaviotruzzi
 */

#include "LtiBandPass.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>

LtiBandPass::LtiBandPass(IplImage* src) {
    aux = cvCreateImage( cvGetSize(src), 8, 3);
    y = cvCreateImage( cvGetSize(src), 8, 1);
    cb = cvCreateImage( cvGetSize(src), 8, 1);
    cr = cvCreateImage( cvGetSize(src), 8, 1);
    thresholded = cvCreateImage(cvGetSize(src), 8, 1);
    Element = cvCreateStructuringElementEx( 5, 5, 3, 3, CV_SHAPE_ELLIPSE);
}


LtiBandPass::~LtiBandPass() {
    cvReleaseImage(&aux);
    cvReleaseImage(&y);
    cvReleaseImage(&cb);
    cvReleaseImage(&cr);

}

void LtiBandPass::Read(IplImage* src) {
    cvCopy(src,aux);
}

void LtiBandPass::setBand(int ymin, int ymax, int cbmin,
                          int cbmax, int crmin, int crmax) {
    this->ymin = ymin;
    this->ymax = ymax;
    this->cbmin = cbmin;
    this->cbmax = cbmax;
    this->crmin = crmin;
    this->crmax = crmax;
}

void LtiBandPass::threshold(IplImage* dst) {

    cvCvtColor(aux,aux,CV_RGB2YCrCb);
    cvSplit(aux,y,cb,cr,NULL);

    cvThreshold(y, y, ymax, 255, CV_THRESH_TOZERO_INV );
    cvThreshold(y, y, ymin, 255, CV_THRESH_BINARY );

    cvThreshold(cr, cr, crmax, 255, CV_THRESH_TOZERO_INV );
    cvThreshold(cr, cr, crmin, 255, CV_THRESH_BINARY );

    cvThreshold(cb, cb, cbmax, 255, CV_THRESH_TOZERO_INV );
    cvThreshold(cb, cb, cbmin, 255, CV_THRESH_BINARY );
    cvMul(y,cb,thresholded);

    cvMul(cr, thresholded, thresholded);
    cvConvertScale(cr, cr, 1, 0);


}
void LtiBandPass::ApplyMorph() {

	cvErode(thresholded, thresholded, Element);
	cvDilate(thresholded, thresholded, Element);
	cvErode(thresholded, thresholded, Element);
	cvDilate(thresholded, thresholded, Element);

}
